home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_09 / welstead / cwdlg.cpp < prev    next >
C/C++ Source or Header  |  1995-05-20  |  4KB  |  148 lines

  1. // File CWDLG.CPP  Dialog classes
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. #include "cwdlg.h"
  7. #include "dlgids.h"
  8.  
  9. HINSTANCE gdlg_instance = 0;
  10. // This must be set to current instance before
  11. // using any dialogs.
  12.  
  13. int check_gdlg_instance () {
  14.     if (!gdlg_instance) {
  15.        MessageBox (NULL,"gdlg_instance not set.",
  16.         "UWDLG.CPP",MB_ICONEXCLAMATION | MB_OK);
  17.        return 0; }
  18.     return 1; }
  19.  
  20. tdialog *this_dialog = 0;
  21.  
  22. BOOL CALLBACK _export tdialog_proc(HWND hdlg,
  23.     UINT message,WPARAM wParam, LPARAM lParam) {
  24.     return this_dialog->handle_message(hdlg,message,
  25.          wParam,lParam); }
  26.  
  27. tdialog::tdialog (HWND Parent,LPCSTR resource_name,
  28.         LPCSTR caption_name) {
  29.     hwndParent = Parent;
  30.     rc_title = resource_name;
  31.     caption_title = caption_name; }
  32.  
  33. void tdialog::center_dialog () {
  34.      RECT dlg_rect;
  35.      int x_screen = GetSystemMetrics(SM_CXSCREEN),
  36.     y_screen = GetSystemMetrics(SM_CYSCREEN);
  37.      GetWindowRect (hdialog,&dlg_rect);
  38.      int dlg_width = dlg_rect.right - dlg_rect.left,
  39.          dlg_ht = dlg_rect.bottom - dlg_rect.top;
  40.      int x_pos = (x_screen - dlg_width)/2,
  41.          y_pos = (y_screen - dlg_ht)/2;
  42.      SetWindowPos (hdialog,NULL,x_pos,y_pos,0,0,
  43.           SWP_NOSIZE|SWP_NOZORDER);}
  44.  
  45. BOOL tdialog::respond_wm_initdialog () {
  46.      center_dialog();
  47.      return 1;}
  48.  
  49. BOOL tdialog::respond_wm_command (WPARAM wParam,LPARAM) {
  50.     switch (LOWORD(wParam)) {
  51.     case IDOK:
  52.          dlg_return_value = 1; 
  53.          EndDialog(hdialog, TRUE);
  54.          return 1;
  55.     case IDCANCEL:
  56.          dlg_return_value = 0;
  57.      EndDialog(hdialog,FALSE);
  58.      return 0;
  59.     }  // end switch  
  60.     return 0; }
  61.  
  62. BOOL tdialog::handle_message (HWND hwnd,UINT message,
  63.      WPARAM wParam,LPARAM lParam) {
  64.     hdialog = hwnd; // hdialog must be set before any
  65.          // handling any messages
  66.     switch (message) {
  67.     case WM_INITDIALOG:
  68.          return respond_wm_initdialog ();
  69.     case WM_COMMAND:
  70.          return respond_wm_command (wParam,lParam);
  71.     }  //  end switch  
  72.     return FALSE; }
  73.  
  74. BOOL tdialog::exec_dialog () {
  75.     if (!check_gdlg_instance()) return 0;
  76.     tdialog *old_tdialog = this_dialog;
  77.     this_dialog = this;
  78.     lpDialogProc =
  79.     (DLGPROC)MakeProcInstance((FARPROC)tdialog_proc,
  80.          gdlg_instance);
  81.     DialogBox(gdlg_instance,rc_title, hwndParent,
  82.        lpDialogProc);
  83.     FreeProcInstance((FARPROC)lpDialogProc);
  84.     this_dialog = old_tdialog;
  85.     return dlg_return_value; }
  86.  
  87. tdialog::~tdialog() { }
  88.  
  89. tinput_dialog *this_input_dialog = 0;
  90.  
  91. BOOL CALLBACK _export tinput_dialog_proc(HWND hdlg,
  92.        UINT message,WPARAM wParam, LPARAM lParam) {
  93.     return this_input_dialog->handle_message(hdlg,
  94.         message,wParam,lParam); }
  95.  
  96. tinput_dialog::tinput_dialog (HWND Parent,
  97.        LPCSTR caption_name,LPCSTR input_name,
  98.        LPCSTR init_text):tdialog(Parent,
  99.        "INPUT_DIALOG",caption_name){
  100.     input_id = ID_DLG_INPUT;
  101.     input_caption_id = ID_DLG_INPUT_CAPTION;
  102.     input_caption = input_name;
  103.     strncpy (input_text,init_text,MAX_INPUT_LENGTH);
  104.     input_text[MAX_INPUT_LENGTH] = '\0'; };
  105.  
  106. BOOL tinput_dialog::respond_wm_initdialog () {
  107.     SetWindowText (hdialog,caption_title);
  108.     SetWindowText (GetDlgItem(hdialog,
  109.         input_caption_id),input_caption);
  110.     SetWindowText(GetDlgItem(hdialog,input_id),
  111.         input_text);
  112.     SetFocus(GetDlgItem(hdialog,input_id));
  113.     return tdialog::respond_wm_initdialog();}
  114.  
  115. BOOL tinput_dialog::respond_wm_command (WPARAM wParam,
  116.         LPARAM) {
  117.     switch (LOWORD(wParam)) {
  118.     case IDOK:
  119.      GetDlgItemText(hdialog,input_id,input_text,
  120.         MAX_INPUT_LENGTH);
  121.          dlg_return_value = 1; 
  122.      EndDialog(hdialog, TRUE);
  123.          return 1;
  124.     case IDCANCEL:
  125.          dlg_return_value = 0;
  126.      EndDialog(hdialog,FALSE);
  127.      return 0;
  128.     }  // end switch  
  129.     return 0; }
  130.  
  131. BOOL tinput_dialog::exec_dialog () {
  132.     if (!check_gdlg_instance()) return 0;
  133.     tinput_dialog *old_tinput_dialog =
  134.         this_input_dialog;
  135.     this_input_dialog = this;
  136.     lpDialogProc =
  137.     (DLGPROC)MakeProcInstance(
  138.         (FARPROC)tinput_dialog_proc,gdlg_instance);
  139.     DialogBox(gdlg_instance,rc_title, hwndParent,
  140.        lpDialogProc);
  141.     FreeProcInstance((FARPROC)lpDialogProc);
  142.     this_input_dialog = old_tinput_dialog;
  143.     return dlg_return_value; }
  144.  
  145. tinput_dialog::~tinput_dialog() { }
  146.  
  147.  
  148.